import { assign } from 'lodash'
import { Message } from 'view-design'
import {
  fetchTableData,
  fetchMeterTypes,
  fetchMeterSectors,
  fetchMeterModels,
  fetchMeterGasClass,
  saveData,
  fetchMeterUnitDetail
} from '@/api/meterManageQuery'

const defaultState = () => {
  return {
    tablePageParams: {
      pageNum: 1,
      pageSize: 20
    },
    pageTotal: 0,
    tableLoading: false,
    tableData: null,
    meterTypeList: [],
    gasClassList: [],
    sectorList: [],
    modelList: [],
    saveBtnLoading: false,
    currentMeterId: undefined
  }
}

export default {
  namespaced: true,
  name: 'meterManageQuery',
  state: defaultState(),
  mutations: {
    SET_TABLE_DATA(state, data) {
      const { list, total } = data
      state.tableData = list
      state.pageTotal = total
    },
    SET_METER_TYPE(state, data) {
      state.meterTypeList = data
    },
    SET_GAS_CLASS(state, data) {
      state.gasClassList = data
    },
    SET_METER_SECTOR(state, data) {
      state.sectorList = data
    },
    SET_MODEL(state, data) {
      state.modelList = data
    },
    SET_TABLE_LOADING(state, flag) {
      state.tableLoading = flag
    },
    SET_CURRENT_METER_ID(state, meterId) {
      state.currentMeterId = meterId
    },
    SET_TABLE_PAGE_PARAMS(state, data) {
      state.tablePageParams = {
        ...state.tablePageParams,
        ...data
      }
    },
    REST_STATE(state) {
      state = assign(state, defaultState())
    }
  },
  actions: {
    async loadTableData(ctx, params) {
      ctx.commit('SET_TABLE_LOADING', true)
      const { code, date } = await fetchTableData({
        ...ctx.state.tablePageParams,
        ...params
      })
      ctx.commit('SET_TABLE_LOADING', false)
      if (code === 200) {
        ctx.commit('SET_TABLE_DATA', date)
      }
    },
    async loadMeterType(ctx, params) {
      const { code, date } = await fetchMeterTypes(params)
      if (code === 200) {
        ctx.commit('SET_METER_TYPE', date)
      }
    },
    async loadGasClass(ctx, params) {
      const { code, date } = await fetchMeterGasClass(params)
      if (code === 200) {
        ctx.commit('SET_GAS_CLASS', date)
      }
    },
    async loadMeterSector(ctx, params) {
      const { code, date } = await fetchMeterSectors(params)
      if (code === 200) {
        ctx.commit('SET_METER_SECTOR', date)
      }
    },
    async loadModel(ctx, params) {
      const { code, date } = await fetchMeterModels(params)
      if (code === 200) {
        ctx.commit('SET_MODEL', date)
      }
    },
    async saveData(ctx, params) {
      const { code, msg } = await saveData(params)
      if (code === 200) {
        Message.success(msg)
      } else {
        Message.error(msg)
      }
    },
    async loadMeterUtilDetail(ctx, params) {
      const data = await fetchMeterUnitDetail(ctx.state.currentMeterId)
      return data
    },
    changeMeterId(ctx, meterId) {
      ctx.commit('SET_CURRENT_METER_ID', meterId)
    },
    changeTablePageParams(ctx, data) {
      ctx.commit('SET_TABLE_PAGE_PARAMS', data)
    },
    clearStore(ctx) {
      ctx.commit('REST_STATE')
    }
  }
}
